<?php
/**
 * Description of afcollection
 *
 * @author Bojan Mitrovic
 */
require_once('../config.php');
require_once('lib.php');
require_once($CFG->dirroot.'/course/format/aformat/newaformatgradeform.php');
class afcollection {
    var $userData;
    var $courseId;
    public function __construct($userId, $courseId) {
        $this->userData = get_record("user", "id", $userId);
        $this->courseId = $courseId;
    }
    public function reportScore($fId,$uId){
        global $CFG;
        $score = 0;
        $scoreSQL = "SELECT themeid, (MIN(posted)-datestart) AS firstPostAfter, datestart, penalpointsafterdays, maxpoints FROM {$CFG->prefix}aformat_forum_posts, {$CFG->prefix}aformat_forum_themes WHERE themeid={$CFG->prefix}aformat_forum_themes.id  AND forumid=$fId AND userid=$uId GROUP BY themeid";
        if ($rsScore=get_recordset_sql($scoreSQL)) {
            while($recordScore = rs_fetch_next_record($rsScore)){
                $score += $recordScore->maxpoints;
                $interval = (int)($recordScore->firstPostAfter)/086400;
                $penals = explode(";",$recordScore->penalpointsafterdays);
                foreach($penals as $penal){
                    $dp = explode(":",$penal);
                    if($interval>$dp[0]){
                        $score-= $dp[1];
                        break;
                    }
                }
            }
            $rsScore->close();
        }
        return $score;
    }
    public function collectionTable($forumId) {
        global $CFG;
        global $USER;
        $totalScore = 0;
        echo '<table>';
        if ($rsGrades=get_recordset_sql("select {$CFG->prefix}aformat_grade_type.* from {$CFG->prefix}aformat_forum_grade_types, {$CFG->prefix}aformat_grade_type where forumid=$forumId and gradetypeid={$CFG->prefix}aformat_grade_type.id")) {
            if(isediting($this->courseId)){
                $mform = new newaformatgradeform("$CFG->wwwroot/course/view.php?id=$this->courseId&sesskey=$USER->sesskey&aformatcollection={$this->userData->id}");
            }
            while($recordGrade=rs_fetch_next_record($rsGrades)) {
                $grd = get_record("aformat_grades", "forumid", $forumId, "userid", $this->userData->id, "gradetypeid", $recordGrade->id)->grade;
                echo '<tr>';
                echo '<td align="right">';
                echo "{$recordGrade->type}: ";
                echo '</td>';
                echo '<td align="right">';
                echo $grd;
                echo '</td>';
                echo '</tr>';
                if(isediting($this->courseId)){
                    $mform->redefine($forumId,$this->userData->id,$recordGrade->id,$grd,$recordGrade->type);
                }                
                $totalScore+=$grd;
            }
            $rsGrades->close();
            if(isediting($this->courseId)){
                $mform->add_action_buttons();
                $mform->display();
                if ($mform->is_cancelled()) {

                } else if ($fromform=$mform->get_data()) {
                    foreach($fromform as $key=>$value){
                        if(strstr($key,'aformatgrade')){
                            $params = explode('n', str_replace('aformatgrade', '', $key));
                            delete_records("aformat_grades", "forumid", $params[0], "userid", $params[1], "gradetypeid", $params[2]);
                            $afGradeRecord = new object();
                            $afGradeRecord->forumid = $params[0];
                            $afGradeRecord->userid = $params[1];
                            $afGradeRecord->gradetypeid = $params[2];
                            $afGradeRecord->grade = $value;
                            insert_record('aformat_grades', $afGradeRecord);
                        }
                    }
                }
            }            
        }
        $grd = $this->reportScore($forumId, $this->userData->id);
        echo '<tr>';
        echo '<td align="right">';
        echo "Redovnost: ";
        echo '</td>';
        echo '<td align="right" width="30px">';
        echo $grd;
        echo '</td>';
        echo '</tr>';
        $totalScore+=$grd;
        echo '<tr style="border-top:solid black 1px;">';
        echo '<td align="right">';
        echo "Total: ";
        echo '</td>';
        echo '<td align="right">';
        echo $totalScore;
        echo '</td>';
        echo '</tr>';
        echo "</table>";
        $tColumns = 0;        
        $forumPostsSQL = "select {$CFG->prefix}aformat_forum.course, {$CFG->prefix}aformat_forum_themes.subject, {$CFG->prefix}aformat_forum_posts.image, {$CFG->prefix}aformat_forum_posts.id from {$CFG->prefix}aformat_forum, {$CFG->prefix}aformat_forum_themes, {$CFG->prefix}aformat_forum_posts where {$CFG->prefix}aformat_forum_posts.userid={$this->userData->id} AND {$CFG->prefix}aformat_forum_themes.id={$CFG->prefix}aformat_forum_posts.themeid AND {$CFG->prefix}aformat_forum_themes.forumid=$forumId AND {$CFG->prefix}aformat_forum.id=$forumId ORDER BY {$CFG->prefix}aformat_forum_themes.id ASC, {$CFG->prefix}aformat_forum_posts.id";
        if ($rs=get_recordset_sql($forumPostsSQL)) {
            echo '<table>';
            while ($record = rs_fetch_next_record($rs)) {
                if($record->image!=NULL&&$record->image!=''){
                    if($tColumns == 0){
                        echo "<tr>";
                    }
                    echo '<td valign="top">';
                    echo '<a target="_blank" href="'.$CFG->wwwroot.'/file.php/'.$record->course.'/aformatdata/'.$this->userData->id.'/'.$record->id.'/'.$record->image.'">';
                    echo '<img border="0" title="'.$record->subject.'" src="'.$CFG->wwwroot.'/file.php/'.$record->course.'/aformatdata/'.$this->userData->id.'/'.$record->id.'/'.$record->image.'" width="130" />';
                    echo "</a>";
                    echo "</td>";
                    $tColumns++;
                    if($tColumns > 2){
                        $tColumns=0;
                        echo "</tr>";
                    }
                }
            }
            $rs->close();
            if($tColumns != 0){
                echo "</tr>";
            }
            echo "</table>";
        }
    }
    public function collections() {
        global $CFG;
        echo '<table style="border:solid silver 1px">';
        echo '<tr>';
        echo '<td colspan="3">';
        echo $this->userData->firstname.' '.$this->userData->lastname.' '.$this->userData->idnumber;
        $forumIdSQL.= "select distinct {$CFG->prefix}aformat_forum.* from {$CFG->prefix}aformat_forum, {$CFG->prefix}aformat_forum_themes, {$CFG->prefix}aformat_forum_posts ";
        $forumIdSQL.= "where {$CFG->prefix}aformat_forum_posts.userid={$this->userData->id} ";
        $forumIdSQL.= "AND {$CFG->prefix}aformat_forum_themes.id={$CFG->prefix}aformat_forum_posts.themeid ";
        $forumIdSQL.= "AND {$CFG->prefix}aformat_forum.id={$CFG->prefix}aformat_forum_themes.forumid ";
        $forumIdSQL.= "ORDER BY {$CFG->prefix}aformat_forum.id ASC";
        if ($rs=get_records_sql($forumIdSQL)) {
            echo '<tr>';
            echo '<td colspan="3">';
            echo '<hr />';
            echo '</td>';
            echo '</tr>';
            foreach($rs as $r) {
                echo '<tr>';
                echo '<td colspan="3">';
                echo $r->name;
                echo '</td>';
                echo '</tr>';
                echo '<tr>';
                echo '<td colspan="3">';
                echo '<hr />';
                echo '</td>';
                echo '</tr>';
                echo '<tr>';
                echo '<td colspan="3">';
                $this->collectionTable($r->id);
                echo '</td>';
                echo '</tr>';
                echo '<tr>';
                echo '<td colspan="3">';
                echo '<hr />';
                echo '</td>';
                echo '</tr>';
            }
        }
        echo '</td>';
        echo '</tr>';
        echo '</table>';
    }
}
?>
